import type { Point } from "/@/components/types";
import type { MovePointOptions, MovePointResult } from "./useRectDrag";
import { getRotatePoint } from "/@/utils/draw";

export default function ({ rotatePoints, newPoint, rotate }: MovePointOptions): MovePointResult {
  //拖动后的中心点
  const rotateCenterPoint = {
    x: rotatePoints[0].x + (newPoint.x - rotatePoints[0].x) / 2,
    y: rotatePoints[0].y + (newPoint.y - rotatePoints[0].y) / 2,
  };
  //原始点
  const originalPoint = getRotatePoint(rotateCenterPoint, newPoint, 360 - rotate);
  //静态原始点
  const staticPoint = getRotatePoint(rotateCenterPoint, rotatePoints[0], 360 - rotate);
  let width = originalPoint.x - staticPoint.x;
  let height = originalPoint.y - staticPoint.y;
  if (width >= 0 && height >= 0) {
    return {
      newX: staticPoint.x,
      newY: staticPoint.y,
      newWidth: width,
      newHeight: height,
    };
  } else if (width >= 0 && height <= 0) {
    return {
      newWidth: width,
      newX: staticPoint.x,
      newHeight: -height,
      newY: staticPoint.y - -height,
    };
  } else if (width <= 0 && height >= 0) {
    return {
      newWidth: -width,
      newX: staticPoint.x - -width,
      newHeight: height,
      newY: staticPoint.y,
    };
  } else {
    return {
      newY: originalPoint.y,
      newWidth: -width,
      newX: originalPoint.x,
      newHeight: -height,
    };
  }
}
